home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / apps / ILBMDemo / ILBMDemo.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  11KB  |  420 lines

  1. /* ILBMDemo.c  07/92   C. Scheppner CBM
  2.  *
  3.  * Demonstrates displaying an ILBM, loading a brush,
  4.  *   saving an ILBM, and optionally printing a screen (CTRL-p)
  5.  *   Use -c (or -c1, -c2, etc) as filename to read from or save to clipboard.
  6.  *
  7.  * requires linkage with several iffp modules - see Makefile
  8.  */
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. #include "iffp/ilbmapp.h"
  12.  
  13.  
  14. #ifdef __SASC
  15. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  16. #else
  17. #ifdef LATTICE
  18. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  19. #endif
  20. #endif
  21.  
  22.  
  23. void chkmsg(void);
  24. void cleanup(void);
  25. void bye(UBYTE *s,int error);
  26.  
  27. #define SAVECHANGES
  28.  
  29. #define MINARGS 3
  30.  
  31. #include "ilbmdemo_rev.h"
  32. UBYTE vers[] = VERSTAG;
  33. UBYTE Copyright[] = VERS " Demo load, save, etc. - Freely Redistributable";
  34.  
  35.  
  36. char *usage =
  37. "Usage: ILBMDemo sourceilbm destilbm [brushname]  (CTRL-p to print screen)\n"
  38. "Displays source, optionally loads and blits brush, saves to dest\n"
  39. "Use filename -c[unit] (ie. -c, -c1, -c2, etc.) for clipboard\n";
  40.  
  41. char *savename;
  42.  
  43. struct Library *IntuitionBase  = NULL;
  44. struct Library *GfxBase        = NULL;
  45. struct Library *IFFParseBase   = NULL;
  46.  
  47. /* Note - these fields are also available in the ILBMInfo structure */
  48. struct   Screen         *scr;         /* for ptr to screen structure */
  49. struct   Window         *win;         /* for ptr to window structure */
  50. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  51. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  52.  
  53. struct   IntuiMessage   *msg;
  54.  
  55. struct   NewWindow      mynw = {
  56.    0, 0,                                  /* LeftEdge and TopEdge */
  57.    0, 0,                                /* Width and Height */
  58.    (UBYTE)-1, (UBYTE)-1,                  /* DetailPen and BlockPen */
  59.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  60.    WFLG_BACKDROP | WFLG_BORDERLESS |
  61.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  62.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  63.    NULL, NULL,                            /* Gadget and Image pointers */
  64.    NULL,                                  /* Title string */
  65.    NULL,                                  /* Screen ptr null till opened */
  66.    NULL,                                  /* BitMap pointer */
  67.    50, 20,                                /* MinWidth and MinHeight */
  68.    0 , 0,                                 /* MaxWidth and MaxHeight */
  69.    CUSTOMSCREEN                           /* Type of window */
  70.    };
  71.  
  72.  
  73. BOOL   FromWb, Done;
  74.  
  75.  
  76. /* ILBM Property chunks to be grabbed
  77.  * List BMHD, CMAP and CAMG first so we can skip them when we write
  78.  * the file back out (they will be written out with separate code)
  79.  */
  80. LONG    ilbmprops[] = {
  81.         ID_ILBM, ID_BMHD,
  82.         ID_ILBM, ID_CMAP,
  83.         ID_ILBM, ID_CAMG,
  84.         ID_ILBM, ID_CCRT,
  85.         ID_ILBM, ID_AUTH,
  86.         ID_ILBM, ID_Copyright,
  87.         TAG_DONE
  88.         };
  89.  
  90. /* ILBM Collection chunks (more than one in file) to be gathered */
  91. LONG    ilbmcollects[] = {
  92.         ID_ILBM, ID_CRNG,
  93.         TAG_DONE
  94.         };
  95.  
  96. /* ILBM Chunk to stop on */
  97. LONG    ilbmstops[] = {
  98.         ID_ILBM, ID_BODY,
  99.         TAG_DONE
  100.         };
  101.  
  102.  
  103. /* For test of adding new chunks to saved FORM */
  104. struct Chunk newchunks[2] = {
  105.     {
  106.     &newchunks[1],
  107.     ID_ILBM, ID_AUTH, IFFSIZE_UNKNOWN,
  108.     "CAS_CBM"},
  109.     {
  110.     NULL,
  111.     ID_ILBM, ID_NAME, IFFSIZE_UNKNOWN,
  112.     "Untitled No. 27"},
  113.     };
  114.  
  115.  
  116. UBYTE nomem[]  = "Not enough memory\n";
  117. UBYTE noiffh[] = "Can't alloc iff\n";
  118.  
  119. /* our indexes to reference our frames
  120.  * DEFault, BRUsh, and SCReen
  121.  */
  122. #define DEF    0
  123. #define BRU    1
  124. #define SCR    2
  125. #define UICOUNT 3
  126.  
  127. /* For our ILBM frames */
  128. struct ILBMInfo  *ilbms[UICOUNT]  = { NULL };
  129.  
  130.  
  131. /* 
  132.  * MAIN 
  133.  */
  134. void main(int argc, char **argv)
  135.    {
  136. #ifdef SAVECHANGES
  137.    struct Chunk *chunk;
  138.    CamgChunk *camg;
  139.    LONG saverror;
  140. #endif
  141.    UBYTE *ilbmname=NULL, *brushname=NULL, ans, c;
  142.    BPTR lock;
  143.    LONG error;
  144.  
  145.    FromWb = argc ? FALSE : TRUE;
  146.  
  147.    if((argc<MINARGS)||(argv[argc-1][0]=='?'))
  148.     {
  149.     printf("%s\n%s\n",Copyright,usage);
  150.         bye("",RETURN_OK);
  151.     }
  152.  
  153.    switch(argc)
  154.       {
  155.       case 4:
  156.          brushname    = argv[3];
  157.       case 3:
  158.          savename    = argv[2];
  159.          ilbmname    = argv[1];
  160.          break;
  161.       }
  162.  
  163.    /* if dest not clipboard, warn if dest file already exists */
  164.    if(strcmp(savename,"-c"))
  165.     {
  166.     if(lock = Lock(savename,ACCESS_READ))
  167.         {
  168.         UnLock(lock);
  169.         printf("Dest file \"%s\" already exists.  Overwrite (y or n) ? ",
  170.             savename);
  171.         ans = 0;
  172.         while((c = getchar()) != '\n') if(!ans)  ans = c | 0x20;
  173.         if(ans == 'n')   bye("Exiting.\n",RETURN_OK);
  174.         }
  175.     }
  176.         
  177.    /* Open Libraries */
  178.  
  179.    if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  180.       bye("Can't open intuition library.\n",RETURN_WARN);
  181.       
  182.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  183.       bye("Can't open graphics library.\n",RETURN_WARN);
  184.  
  185.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  186.       bye("Can't open iffparse library.\n",RETURN_WARN);
  187.  
  188.  
  189.  
  190. /* 
  191.  * Alloc three ILBMInfo structs (one each for defaults, screen, brush) 
  192.  */
  193.     if(!(ilbms[0] = (struct ILBMInfo *)
  194.     AllocMem(UICOUNT * sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  195.         bye(nomem,RETURN_FAIL);
  196.     else 
  197.     {
  198.     ilbms[BRU] = ilbms[0] + 1;
  199.     ilbms[SCR] = ilbms[0] + 2;
  200.     }
  201.  
  202. /*
  203.  * Here we set up default ILBMInfo fields for our
  204.  * application's frames.
  205.  * Above we have defined the propery and collection chunks
  206.  * we are interested in (some required like BMHD)
  207.  * Since all of our frames are for ILBM's, we'll initialize
  208.  * one default frame and clone the others from it.
  209.  */
  210.     ilbms[DEF]->ParseInfo.propchks    = ilbmprops;
  211.     ilbms[DEF]->ParseInfo.collectchks    = ilbmcollects;
  212.     ilbms[DEF]->ParseInfo.stopchks    = ilbmstops;
  213.  
  214.     ilbms[DEF]->windef    = &mynw;
  215. /* 
  216.  * Initialize our working ILBM frames from our default one
  217.  */
  218.     *ilbms[SCR] = *ilbms[DEF];    /* for our screen */
  219.     *ilbms[BRU] = *ilbms[DEF];    /* for our brush  */
  220.  
  221. /* 
  222.  * Alloc two IFF handles (one for screen frame, one for brush frame) 
  223.  */
  224.     if(!(ilbms[SCR]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  225.     if(!(ilbms[BRU]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  226.  
  227. /* Load and display an ILBM
  228.  */
  229.     if(error = showilbm(ilbms[SCR],ilbmname))
  230.     {
  231.     printf("Can't load background \"%s\"\n",ilbmname);
  232.     bye("",RETURN_WARN);
  233.     }
  234.  
  235.     /* These were set up by our successful showilbm() above */
  236.     win = ilbms[SCR]->win;    /* our window */
  237.     wrp = ilbms[SCR]->wrp;    /* our window's RastPort */
  238.     scr = ilbms[SCR]->scr;    /* our screen */
  239.     vp  = ilbms[SCR]->vp;        /* our screen's ViewPort */
  240.  
  241.     ScreenToFront(scr);
  242.  
  243.  
  244.  /* Now let's load a brush and blit it into the window
  245.   */
  246.     if(brushname)
  247.     {
  248.     if (error = loadbrush(ilbms[BRU],brushname))
  249.         {
  250.         printf("Can't load brush \"%s\"\n",brushname);
  251.         bye("",RETURN_WARN);
  252.         }
  253.     else    /* Success */
  254.         {
  255.         D(bug("About to Blt bitmap $%lx to rp $%lx, w=%ld h=%ld\n",
  256.         ilbms[BRU]->brbitmap,wrp,ilbms[BRU]->Bmhd.w,ilbms[BRU]->Bmhd.h));
  257.             BltBitMapRastPort(ilbms[BRU]->brbitmap,0,0,
  258.                              wrp,0,0,
  259.                              ilbms[BRU]->Bmhd.w, ilbms[BRU]->Bmhd.h,
  260.                              0xC0);
  261.              }
  262.     }
  263.  
  264. #ifdef SAVECHANGES
  265.  
  266.  /* This code is an example for Read/Modify/Write programs
  267.   *
  268.   * We copy off the parsed chunks we want to preserve,
  269.   * close the IFF read file, reopen it for write,
  270.   * and save a new ILBM which
  271.   * will include the chunks we have preserved, but
  272.   * with newly computed and set-up BMHD, CMAP, and CAMG.
  273.   */ 
  274.  
  275.    if(!(ilbms[SCR]->ParseInfo.copiedchunks =
  276.     copychunks(ilbms[SCR]->ParseInfo.iff,
  277.            ilbmprops, ilbmcollects,
  278.            MEMF_PUBLIC)))
  279.         printf("error cloning chunks\n");
  280.    else
  281.     {
  282.         /* we can close the file now */
  283.        closeifile(ilbms[SCR]);
  284.  
  285.        printf("Test of copychunks and findchunk:\n");
  286.  
  287.        /* Find copied CAMG chunk if any */
  288.        if(chunk = findchunk(ilbms[SCR]->ParseInfo.copiedchunks,ID_ILBM,ID_CAMG))
  289.         {
  290.         camg = (CamgChunk *)chunk->ch_Data;
  291.         printf("CAMG: $%08lx\n",camg->ViewModes);
  292.         }
  293.         else printf("No CAMG found\n");
  294.  
  295.        /* Find copied CRNG chunks if any */
  296.        if(chunk = findchunk(ilbms[SCR]->ParseInfo.copiedchunks,ID_ILBM,ID_CRNG))
  297.         {
  298.            while((chunk)&&(chunk->ch_ID == ID_CRNG))
  299.         {
  300.         printf("Found a CRNG chunk\n");
  301.         chunk = chunk->ch_Next;
  302.         }
  303.         }
  304.        else printf("No CRNG chunks found\n");
  305.     }
  306.  
  307.     printf("\nAbout to save screen as %s, adding NAME and AUTH chunks\n",
  308.         savename);
  309.  
  310.     if(saverror = screensave(ilbms[SCR], ilbms[SCR]->scr,
  311.                 ilbms[SCR]->ParseInfo.copiedchunks,
  312.                 newchunks,
  313.                 savename))
  314.             printf("%s\n",IFFerr(saverror));
  315.  
  316. #endif
  317.  
  318.    Done = FALSE;
  319.    while(!Done)
  320.       {
  321.       Wait(1<<win->UserPort->mp_SigBit);
  322.       chkmsg();
  323.       }
  324.  
  325.  
  326.    cleanup();
  327.    exit(RETURN_OK);
  328.    }
  329.  
  330.  
  331. void chkmsg(void)
  332.     {
  333.     LONG  error;
  334.     ULONG class;
  335.     UWORD code;
  336.     WORD  mousex, mousey;
  337.  
  338.     while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  339.     {
  340.     class = msg->Class;
  341.           code  = msg->Code;
  342.           mousex = msg->MouseX;
  343.           mousey = msg->MouseY;
  344.  
  345.           ReplyMsg(msg);
  346.           switch(class)
  347.            {
  348.         case IDCMP_MOUSEBUTTONS:
  349.         switch(code)
  350.         {
  351.         /* emulate a close gadget */
  352.         case SELECTDOWN:
  353.            if((mousex < 12)&&(mousey < 12))    Done = TRUE;
  354.            break;
  355.         default:
  356.            break;
  357.         }
  358.             case IDCMP_VANILLAKEY:
  359.             switch(code)
  360.                    {
  361.         /* also quit on CTRL-C, CTRL-D, or q */
  362.                    case 'q': case 0x04: case 0x03:
  363.                   Done = TRUE;
  364.                   break;
  365.         case 0x10:    /* CTRL-p means print */
  366.  
  367.           /* Print the whole screen */
  368.           if(error=screendump(ilbms[SCR]->scr,
  369.                 0,0,
  370.                 ilbms[SCR]->scr->Width,
  371.                 ilbms[SCR]->scr->Height,
  372.                 0,0))
  373.             printf("Screendump printer error=%ld\n",error);
  374.           break;
  375.             
  376.                    default:
  377.                   break;
  378.         }
  379.             default:
  380.             break;
  381.             }
  382.           }
  383.     }
  384.  
  385.  
  386. void bye(UBYTE *s,int error)
  387.    {
  388.    if((*s)&&(!FromWb)) printf("%s\n",s);
  389.    cleanup();
  390.    exit(error);
  391.    }
  392.  
  393.  
  394. void cleanup()
  395.    {
  396.    if(ilbms[SCR])
  397.     {
  398.        if(ilbms[SCR]->scr)        unshowilbm(ilbms[SCR]);
  399. #ifdef SAVECHANGES
  400.        freechunklist(ilbms[SCR]->ParseInfo.copiedchunks);
  401. #endif
  402.        if(ilbms[SCR]->ParseInfo.iff)    FreeIFF(ilbms[SCR]->ParseInfo.iff);
  403.     }
  404.  
  405.    if(ilbms[BRU])
  406.     {
  407.        if(ilbms[BRU]->brbitmap)    unloadbrush(ilbms[BRU]);
  408.        if(ilbms[BRU]->ParseInfo.iff)     FreeIFF(ilbms[BRU]->ParseInfo.iff);
  409.     }
  410.  
  411.    if(ilbms[0])
  412.     {
  413.     FreeMem(ilbms[0],UICOUNT * sizeof(struct ILBMInfo));
  414.     }
  415.  
  416.    if(GfxBase)          CloseLibrary(GfxBase);
  417.    if(IntuitionBase) CloseLibrary(IntuitionBase);
  418.    if(IFFParseBase)  CloseLibrary(IFFParseBase);
  419.    }
  420.